package com.aimaoblog.front.service;

import com.aimaoblog.constant.ServerResponse;
import com.aimaoblog.front.api.HomeFrontService;
import com.aimaoblog.front.dao.AdminMapper;
import com.aimaoblog.front.dao.ArticleMapper;
import com.aimaoblog.front.dao.TagMapper;
import com.aimaoblog.pojo.Admin;
import com.aimaoblog.pojo.Tag;
import com.aimaoblog.vo.ArticleAndCommentCountVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author PuaChen
 * @Create 2018-07-06 20:35
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class HomeFrontServiceImpl implements HomeFrontService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ServerResponse<List<Tag>> findHotTag() {
        PageHelper.startPage(1, 20);
        return ServerResponse.createSuccessAndData(tagMapper.selectAll());
    }

    @Override
    public ServerResponse<ArticleAndCommentCountVo> getTopArticle() {
        ArticleAndCommentCountVo data = articleMapper.selectTop();
        if (data == null) {
            return ServerResponse.createFailure("没有置顶");
        }
        return ServerResponse.createSuccessAndData(data);
    }

    @Override
    public ServerResponse<Admin> getAdmin() {
        Admin admin = adminMapper.selectByPrimaryKey("admin");
        if (admin == null) {
            return ServerResponse.createFailure("获取失败");
        }
        admin.setPassword(null);
        admin.setId(null);
        admin.setLasttime(null);
        return ServerResponse.createSuccessAndData(admin);
    }
}
